package com.bosicen.crm.web.controller.sysmanage;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bosicen.crm.dao.entity.Company;
import com.bosicen.crm.dao.entity.Order;
import com.bosicen.crm.dao.entity.ProductSery;
import com.bosicen.crm.dao.entity.ProductType;
import com.bosicen.crm.dao.entity.User;
import com.bosicen.crm.service.CommonServiceImpl;
import com.bosicen.crm.tools.AppUtil;
import com.google.gson.Gson;

/***
 * 初始化基础数据
 * @author niyong
 *
 */
@Controller
@RequestMapping("/basicinfo")
public class BasicInfoController {
	
	@Autowired
	public CommonServiceImpl commonService;
	
	/****
	 * 查询产品系列列表
	 * @return
	 */
	@RequestMapping(value="/addproductsery")
	public String addProductSery(HttpServletRequest request){
		return "sysmanage/addProductSery";
	}
	
	/****
	 * 查询产品系列列表
	 * @return
	 */
	@RequestMapping(value="/findproductsery")
	public String findProductSery(HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("userinfo");
		try{
			List<ProductSery> list=this.commonService.findProductSery(user.getCompanyid());
			request.setAttribute("list", list);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "sysmanage/ProductSery_list";
	}
	/****
	 * 新增/修改 产品系列信息
	 * @param ps
	 * @param request
	 * @param type   0：删除   1: 新增/修改
	 */
	@RequestMapping(value="/cudproductsery/{type}")
	public void cudProductSery(ProductSery ps,HttpServletRequest request,@PathVariable("type") Integer type,Writer writer){
		Gson gson=new Gson();
		boolean flag=false;
		try{
			User user=(User) request.getSession().getAttribute("userinfo");
			ps.setCompanyid(user.getCompanyid());
			switch(type){
				case 0: flag=this.commonService.DelProductSery(ps);break;
				case 1: flag=this.commonService.SUProductSery(ps);break;
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	/****
	 * 查询产品类型列表
	 * @return
	 */
	@RequestMapping(value="/findproducttype")
	public String findProductType(HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("userinfo");
		try{
			List<ProductType> list=this.commonService.findProductType(user.getCompanyid());
			request.setAttribute("list", list);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "sysmanage/ProductType_list";
	}
	/****
	 * 新增/修改 产品类型信息
	 * @param ps
	 * @param request
	 */
	@RequestMapping(value="/cudproducttype/{type}")
	public void cudProductType(ProductType ps,HttpServletRequest request,@PathVariable("type") Integer type,Writer writer){
		Gson gson=new Gson();
		boolean flag=false;
		try{
			User user=(User) request.getSession().getAttribute("userinfo");
			ps.setCompanyid(user.getCompanyid());
			switch(type){
				case 0: flag=this.commonService.DelProductType(ps);break;
				case 1: flag=this.commonService.SUProductType(ps);break;
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 新增,修改经销商信息
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addcompany2/{type}")
	public String addcompany(HttpServletRequest request,@PathVariable("type") Integer type){
		String id = request.getParameter("id");
		Company company = null;
		List<Company> list = null;
		if(type != 0 && StringUtils.isNotBlank(id)){
			list = this.commonService.findCompany(AppUtil.ObjectToInt(id));
			if(!list.isEmpty()){
				company = list.get(0);
			}
		}
		request.setAttribute("company", company);
		return "sysmanage/addcompany";
	}
	
	
	/**
	 * 新增/修改经销商信息
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/cudcompany2/{type}")
	public ModelAndView saveCompany(Company company,HttpServletRequest request,@PathVariable("type") Integer type,Writer writer){
		boolean flag=false;
		String msg = "操作失败！";
		if(company != null){
			company.setCreatedate(new Date());
			company.setCompanynumber(System.currentTimeMillis()+"");
		}
		switch(type){
			case 0: flag=this.commonService.DelCompany(company);break;
			case 1: flag=this.commonService.SUCompany(company);break;
		}
		if(flag)msg = "操作成功！";
		request.setAttribute("msg", msg);
		return new ModelAndView("redirect:/basicinfo/companylist2");
	}
	
	
	@RequestMapping(value = "/companylist2")
	public String findCompanylist(HttpServletRequest request) {
		List list = this.commonService.findCompanylist();
		request.setAttribute("list", list);
		return "sysmanage/company_list";
	}
	
}
